package ljl.codetop;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class _15_3sum {

    /**
     * todo 超时了
     */
    static class trytry {

        public List<List<Integer>> threeSum(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            Set<List<Integer>> res = new HashSet<>();
            for (int i = 0; i < n; i++) {
                int a = nums[i];
                if (a > 0) continue;
                for (int j = i + 1; j < n; j++) {
                    int b = nums[j];
                    if (a + b > 0 || a + b + nums[n - 1] < 0) continue;
                    for (int k = j + 1; k < n; k++) {
                        int c = nums[k];
                        if (a + b + c == 0) {
                            res.add(Arrays.asList(a, b, c));
                        }
                    }
                }
            }
            return new ArrayList<>(res);
        }
    }

    static class off1 {
        public List<List<Integer>> threeSum(int[] nums) {
            int n = nums.length;
            Arrays.sort(nums);
            List<List<Integer>> ans = new ArrayList<>();
            for (int first = 0; first < n; ++first) {
                if (first > 0 && nums[first] == nums[first - 1]) {
                    continue;
                }
                int third = n - 1;
                int target = -nums[first];
                for (int second = first + 1; second < n; ++second) {
                    if (second > first + 1 && nums[second] == nums[second - 1]) {
                        continue;
                    }
                    while (second < third && nums[second] + nums[third] > target) {
                        --third;
                    }
                    if (second == third) {
                        break;
                    }
                    if (nums[second] + nums[third] == target) {
                        ans.add(Arrays.asList(nums[first], nums[second], nums[third]));
                    }
                }
            }
            return ans;
        }
    }

    /**
     * 一次把值当成下标
     * 一次没排序
     * 第三次才改对，抄都抄错！
     */
    static class copy_off1 {
        public List<List<Integer>> threeSum(int[] nums) {
            Arrays.sort(nums);
            int n = nums.length;
            List<List<Integer>> res = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                int a = nums[i];
                if (i > 0 && a == nums[i - 1]) continue;
                int ci = n - 1;
                for (int j = i + 1; j < n; j++) {
                    int b = nums[j];
                    if (j > i + 1 && b == nums[j - 1]) continue;
                    while (j < ci && b + nums[ci] > -a) ci--;
                    if (j == ci) break;
                    if (b + nums[ci] == -a) res.add(Arrays.asList(a, b, nums[ci]));
                }
            }
            return res;
        }
    }

    /**
     * 这次我不支持路飞，太丑，这代码
     */
    static class lufei {
        public List<List<Integer>> threeSum(int[] nums) {
            Arrays.sort(nums);
            List<List<Integer>> res = new ArrayList<>();
            for(int k = 0; k < nums.length - 2; k++){
                if(nums[k] > 0) break;
                if(k > 0 && nums[k] == nums[k - 1]) continue;
                int i = k + 1, j = nums.length - 1;
                while(i < j){
                    if (i > k + 1 && nums[i] == nums[i - 1]) {
                        i++;
                        continue;
                    }
                    int sum = nums[k] + nums[i] + nums[j];
                    if(sum < 0){
                        i++;
                    } else if (sum > 0) {
                        j--;
                    } else {
                        res.add(Arrays.asList(nums[k], nums[i], nums[j]));
                        i++;
                    }
                }
            }
            return res;
        }
    }
}
